package com.flyco.modules.sms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.modules.sms.entity.SmsGiftInfo;
import com.flyco.modules.sms.model.SmsGiftInfoModel;
import com.flyco.modules.sms.service.ISmsGiftInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 赠品配置表
 * @Author: flyco
 * @Date:   2023-02-03
 * @Version: V1.0
 */
@Slf4j
@Api(tags="赠品配置表")
@RestController
@RequestMapping("/f/sms/smsGiftInfo")
public class SmsGiftInfoController extends BaseController<SmsGiftInfo, ISmsGiftInfoService> {
	@Autowired
	private ISmsGiftInfoService smsGiftInfoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param smsGiftInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "赠品配置表-分页列表查询")
	@ApiOperation(value="赠品配置表-分页列表查询", notes="赠品配置表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SmsGiftInfo smsGiftInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SmsGiftInfo> queryWrapper = QueryGenerator.initQueryWrapper(smsGiftInfo, req.getParameterMap());
		queryWrapper.eq("brand_category",1);
		Page<SmsGiftInfo> page = new Page<SmsGiftInfo>(pageNo, pageSize);
		IPage<SmsGiftInfo> pageList = smsGiftInfoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 */
	@AutoLog(value = "赠品配置表-添加")
	@ApiOperation(value="赠品配置表-添加", notes="赠品配置表-添加")
	@PostMapping(value = "/add")
	@Transactional
	public Result<?> add(@RequestBody SmsGiftInfoModel smsGiftInfoModel) {
		smsGiftInfoModel.setBrandCategory(1);
		String errMsg = smsGiftInfoService.checkRelation2(smsGiftInfoModel);
		if(errMsg!=null){
			return Result.fail(errMsg);
		}

		SmsGiftInfo smsGiftInfo = new SmsGiftInfo();
		BeanUtil.copyProperties(smsGiftInfoModel,smsGiftInfo);
		smsGiftInfoService.save(smsGiftInfo);
		smsGiftInfoService.addRelation(smsGiftInfo.getId(),smsGiftInfoModel);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 */
	@AutoLog(value = "赠品配置表-编辑")
	@ApiOperation(value="赠品配置表-编辑", notes="赠品配置表-编辑")
	@PutMapping(value = "/edit")
	@Transactional
	public Result<?> edit(@RequestBody SmsGiftInfoModel smsGiftInfoModel) {
		String errMsg = smsGiftInfoService.checkRelation2(smsGiftInfoModel);
		if(errMsg!=null){
			return Result.fail(errMsg);
		}

		SmsGiftInfo smsGiftInfo = new SmsGiftInfo();
		BeanUtil.copyProperties(smsGiftInfoModel,smsGiftInfo);
		smsGiftInfoService.updateById(smsGiftInfo);
		smsGiftInfoService.addRelation(smsGiftInfo.getId(),smsGiftInfoModel);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "赠品配置表-通过id删除")
	@ApiOperation(value="赠品配置表-通过id删除", notes="赠品配置表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		smsGiftInfoService.removeById(id);
		smsGiftInfoService.removeRelation(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "赠品配置表-批量删除")
	@ApiOperation(value="赠品配置表-批量删除", notes="赠品配置表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> list = Arrays.asList(ids.split(","));
		this.smsGiftInfoService.removeByIds(list);

		list.forEach(id->{
			smsGiftInfoService.removeRelation(Long.parseLong(id));
		});

		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "赠品配置表-通过id查询")
	@ApiOperation(value="赠品配置表-通过id查询", notes="赠品配置表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		SmsGiftInfo smsGiftInfo = smsGiftInfoService.getById(id);
		return Result.ok(smsGiftInfo);
	}

	 /**
	  * 通过id查询关联的分类
	  *
	  * @param id
	  * @return
	  */
	 @GetMapping(value = "/queryCategoryRelationById")
	 public Result<?> queryCategoryRelationById(@RequestParam(name = "id", required = true) Long id) {
		 return Result.ok(smsGiftInfoService.queryCategoryRelationById(id));
	 }

	 /**
	  * 通过id查询关联的商品
	  *
	  * @param id
	  * @return
	  */
	 @GetMapping(value = "/queryProductRelationById")
	 public Result<?> queryProductRelationById(@RequestParam(name = "id", required = true) Long id) {
		 return Result.ok(smsGiftInfoService.queryProductyRelationById(id));
	 }

	 /**
	  * 通过id查询关联的进销商
	  *
	  * @param id
	  * @return
	  */
	 @GetMapping(value = "/queryStoreRelationById")
	 public Result<?> queryStoreRelationById(@RequestParam(name = "id", required = true) Long id) {
		 return Result.ok(smsGiftInfoService.queryStoreRelationById(id));
	 }

	 /**
	  * 通过id查询关联的进销商分组
	  *
	  * @param id
	  * @return
	  */
	 @GetMapping(value = "/queryStoreGroupRelationById")
	 public Result<?> queryStoreGroupRelationById(@RequestParam(name = "id", required = true) Long id) {
		 return Result.ok(smsGiftInfoService.queryStoreGroupRelationById(id));
	 }

  /**
   * 导出excel
   *
   * @param request
   * @param smsGiftInfo
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, SmsGiftInfo smsGiftInfo) {
      return super.exportXls(request, smsGiftInfo, SmsGiftInfo.class, "赠品配置表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SmsGiftInfo.class);
  }

}
